home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / database / postgres / mpsql-1.001 / mpsql-1~ / mpsql-1.0 / image.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-28  |  8.5 KB  |  264 lines

  1. /************************************************************************/
  2. /* Module : image.c                                    */
  3. /* Purpose: graphics module for mpsql                                    */
  4. /* By     : Keith R. Davis                            */
  5. /* Date   : 9/9/95                                    */
  6. /* Notes  : Modified from source  by Douglas Young                      */
  7. /*          Copyright 1994 by Prentice Hall                        */
  8. /************************************************************************/
  9.  
  10. #include <Xm/Xm.h>        /* motif lib header        */
  11. #include <Xm/PushB.h>        /* drawing area widget header    */
  12. #include <Xm/Label.h>           /* label widget header          */
  13. #include <X11/xpm.h>        /* xpm lib header        */
  14.  
  15. #include "image.h"        /* image module header            */
  16.  
  17. #define MAX(a,b) (a>b ? a : b)
  18. #define PAD 2
  19.  
  20. /************************************************************************/
  21. /* Function: InstallLabeledPixmap                        */
  22. /* Purpose : places a pixmap & label  in a widgets label property     */
  23. /* Params  : w : widget to place pixmap in                        */
  24. /*         xpmDescription : ptr to pixmap data of image to display    */
  25. /* Returns : nothing                                            */
  26. /************************************************************************/
  27.  
  28. void InstallLabeledPixmap ( Widget w, char **xpmDescription )
  29. {
  30.     XmString       label;
  31.     XmFontList     fontlist;
  32.     GC             gc;
  33.     GC             inverseGc;
  34.     Dimension      width, height;
  35.     unsigned int   pixmapWidth, pixmapHeight;
  36.     XGCValues      values;
  37.     int            junk, depth;
  38.     Display       *display = XtDisplay ( w );
  39.     unsigned char  alignment;
  40.     XpmAttributes  attributes;
  41.     XpmColorSymbol symbols[5];
  42.     int            totalWidth, totalHeight;
  43.     int            status;
  44.     Pixmap         labelPixmap, 
  45.                    xpmPixmap;
  46.     Colormap       cmap;
  47.  
  48.    /* Retrieve the values used by the given widget  */
  49.  
  50.     XtVaGetValues ( w, 
  51.                     XmNlabelString, &label,
  52.                     XmNfontList,    &fontlist,
  53.                     XmNforeground,  &values.foreground, 
  54.                     XmNbackground,  &values.background, 
  55.                     XmNdepth,       &depth, 
  56.                     XmNalignment,   &alignment,
  57.                     XmNcolormap,    &cmap,
  58.                     NULL );
  59.    /*
  60.     * Create two GCs, one to draw the text and copy the pixmaps
  61.     * and another that can be used to erase a pixmap by filling
  62.     * it with the background color of the label widget. Because
  63.     * the normal GC is used by XmStringDraw, which modifies
  64.     * the font attribute of the GC, allocate this GC using 
  65.     * XtAllocateGC() and specify GCFont as modifiable.
  66.     */
  67.  
  68.     gc = XtAllocateGC ( w, depth, 
  69.                         GCForeground | GCBackground, 
  70.                         &values, GCFont, 0 );
  71.  
  72.     values.foreground = values.background;
  73.  
  74.     inverseGc = XtGetGC ( w, 
  75.                           GCForeground | GCBackground,
  76.                           &values );
  77.  
  78.    /*
  79.     * Set up the XpmColorSymbol array, binding the name "background"
  80.     * to the actual background color of the widget.
  81.     */
  82.  
  83.     symbols[0].name  = "background";
  84.     symbols[0].value = NULL;
  85.     symbols[0].pixel = values.background;
  86.  
  87.    /*
  88.     * Set the resulting information in the attributes structure
  89.     */
  90.  
  91.     attributes.colorsymbols = symbols;
  92.     attributes.numsymbols   = 1;
  93.  
  94.    /*
  95.     * Specify the visual, colormap and depth 
  96.     * to be used and set the XpmAttributes mask.
  97.     */
  98.  
  99.     attributes.colormap = cmap;
  100.     attributes.depth    = depth;
  101.     attributes.visual   = DefaultVisual ( display,
  102.                                            DefaultScreen ( display ) );
  103.  
  104.     attributes.valuemask = XpmColorSymbols | XpmDepth | 
  105.                            XpmColormap | XpmVisual;
  106.  
  107.    /*
  108.     * Create the pixmap of the given image
  109.     */
  110.  
  111.     status = XpmCreatePixmapFromData ( display,
  112.                                         DefaultRootWindow ( display ), 
  113.                                        xpmDescription,  &xpmPixmap,
  114.                                        NULL, &attributes );
  115.  
  116.    /*
  117.     * Compute the size of the label string and the given pixmap 
  118.    */
  119.  
  120.    XmStringExtent ( fontlist, label, &width, &height );
  121.  
  122.    XGetGeometry ( display, xpmPixmap, ( Window * ) &junk,   
  123.                   (int *) &junk, (int *) &junk, 
  124.                   &pixmapWidth, &pixmapHeight,    
  125.                   ( unsigned int *) &junk, ( unsigned int *) &junk );
  126.  
  127.   /*
  128.    * Compute the sum of the label and pixmap sizes.
  129.    */
  130.  
  131.    totalHeight = pixmapHeight + height + PAD;
  132.    totalWidth = totalHeight;
  133.  
  134.   /*
  135.    * Create the final pixmap using the combined size and 
  136.    * fill the pixmap with the background color of the widget 
  137.    */
  138.  
  139.  
  140.    labelPixmap = 
  141.                XCreatePixmap ( display, 
  142.                                RootWindowOfScreen ( XtScreen ( w ) ),
  143.                                totalWidth, totalHeight, depth );
  144.   
  145.    XFillRectangle ( display, labelPixmap, 
  146.                     inverseGc, 0, 0, 
  147.                     totalWidth, totalHeight );
  148.   
  149.   /*
  150.    * Copy the Xpm-created pixmap into the larger pixmap and
  151.    * then draw the string below the pixmap.
  152.    */
  153.   
  154.    XCopyArea ( XtDisplay ( w ), xpmPixmap, labelPixmap, 
  155.                gc, 0, 0, pixmapWidth, pixmapHeight, 
  156.                ( totalWidth - pixmapWidth ) / 2, 
  157.                0 );
  158.   
  159.    XmStringDraw ( display, labelPixmap, fontlist, label,
  160.                   gc, 0, pixmapHeight + PAD, totalWidth, 
  161.                   alignment, XmSTRING_DIRECTION_L_TO_R, NULL );
  162.  
  163.   /*
  164.    * Install the final pixmap in the widget.
  165.    */
  166.  
  167.    XtVaSetValues ( w,
  168.                    XmNlabelPixmap, labelPixmap,
  169.                    XmNlabelType,   XmPIXMAP,
  170.                    NULL );
  171.                    
  172.   /*
  173.    * Free the GCs, the initial pixmap, and the string retrieved
  174.    * from the label widget.
  175.    */
  176.  
  177.    XFreePixmap ( display, xpmPixmap );
  178.  
  179.    XtReleaseGC ( w, gc);
  180.    XtReleaseGC ( w, inverseGc );
  181.    XmStringFree ( label );
  182. }
  183.  
  184. /************************************************************************/
  185. /* Function: SetupIcon                                        */
  186. /* Purpose : installs pixmap as the apps window manager icon         */
  187. /* Params  : shell : app's shell widget                         */
  188. /*         xpmDesc : ptr to pixmap data of image to display            */
  189. /* Returns : nothing                                            */
  190. /************************************************************************/
  191.  
  192. void SetupIcon ( Widget shell, char **xpmDesc )
  193. {
  194.     Pixmap          pix;
  195.     XpmAttributes   attributes;
  196.     int             status;
  197.     Display        *dpy = XtDisplay ( shell );
  198.     Widget          icon_shell, label;
  199.  
  200.    /*
  201.     * Create a shell widget, and set mappedWhenManaged to FALSE.
  202.     * The windowe manager controls when this window is mapped.
  203.     */
  204.  
  205.     icon_shell = XtVaAppCreateShell ( "icon_shell", "Icon_shell", 
  206.                                       topLevelShellWidgetClass, dpy, 
  207.                                           XmNmappedWhenManaged, FALSE,
  208.                                           NULL );
  209.  
  210.    /*
  211.     * Create a label to display the pixmap
  212.     */
  213.  
  214.     label = XtCreateManagedWidget ( "button", 
  215.                                      xmLabelWidgetClass,
  216.                                      icon_shell, NULL, 0 );
  217.    /*
  218.     * Retrieve the depth and colormap used by this widget
  219.     * and store the results in the corresponding field
  220.     * of an XpmAttributes structure.
  221.     */
  222.  
  223.     XtVaGetValues ( label,
  224.                     XmNdepth,    &attributes.depth,
  225.                     XmNcolormap, &attributes.colormap,
  226.                     NULL);
  227.    /*
  228.     * Specify the visual to be used and set the XpmAttributes mask.
  229.     */
  230.  
  231.     attributes.visual = DefaultVisual ( dpy, DefaultScreen ( dpy ) );
  232.     attributes.valuemask = XpmDepth | XpmColormap | XpmVisual;
  233.  
  234.    /*
  235.     * Create the pixmap
  236.     */
  237.  
  238.     status = XpmCreatePixmapFromData ( dpy,
  239.                                         DefaultRootWindow ( dpy ), 
  240.                                         xpmDesc, &pix, NULL,
  241.                                         &attributes );
  242.    /*
  243.     * Install the pixmap in the label
  244.     */
  245.     
  246.     if ( status == XpmSuccess ) 
  247.        XtVaSetValues ( label, 
  248.                        XmNlabelType,   XmPIXMAP,
  249.                        XmNlabelPixmap, pix,
  250.                        NULL );
  251.    /*
  252.     * Realize the icon shell to force the shellâ•’s window to exist
  253.     */
  254.  
  255.     XtRealizeWidget ( icon_shell );
  256.  
  257.    /*
  258.     * Install the icon_shell's window as the icon window.
  259.     */
  260.  
  261.     XtVaSetValues ( shell, XmNiconWindow, 
  262.                     XtWindow ( icon_shell ), NULL );
  263. }
  264.